{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b336a09b",
   "metadata": {},
   "source": [
    "# 第六周学习笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2db34b4",
   "metadata": {},
   "source": [
    "## 一、列表基本方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0ec18158",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 1]\n",
      "insert之后的names： ['Mike', 'yougege', 'Mary', 'Jan', 'Jack', 1]\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "# 1.增加的方法\n",
    "# 1.1 增加 append()  增加在list的末尾\n",
    "names.append(1)\n",
    "print(names)\n",
    "# 1.2 插入 insert() 可以指定位置增加元素\n",
    "names.insert(1,'yougege')\n",
    "print(\"insert之后的names：\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9dc35857",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove删除的结果： ['Mike', 'Mary', 'Jan', 'Jack']\n",
      "pop()方法后的names: ['Mike', 'Mary', 'Jan']\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "# 2. 删除 remove  和 pop\n",
    "# 优势：如果删除list中多个元素时，remove比较合适\n",
    "\"\"\"\n",
    "Remove first occurrence of value.\n",
    "Raises ValueError if the value is not present.\n",
    "\"\"\"\n",
    "# 2.1 remove()一般情况下 使用remove先用in做判断\n",
    "if 10001 in names:\n",
    "    names.remove(10001)\n",
    "print(\"remove删除的结果：\",names)\n",
    "\n",
    "\n",
    "# 2.2 pop() 删除位置为1的这个元素\n",
    "names.pop(-1)\n",
    "print(\"pop()方法后的names:\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "29dab5e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "# 3. index()索引 查看该元素如10001 在stu_id中第一个出现的位置索引\n",
    "print(stu_id.index(10001))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d0a927f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清除names列表后的值： []\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "# 4. clear()清除所有数据\n",
    "names.clear()\n",
    "print(\"清除names列表后的值：\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b1397d96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count方法的结果: 3\n",
      "['Mike', 'Mary', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "# 5.count()查看列表元素出现的个数，（简单应用）统计分数出现的次数\n",
    "print(\"count方法的结果:\",stu_id.count(10001))\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d03dcfe2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "# 6. extend() 是给指定的list进行扩充\n",
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6bc7af0",
   "metadata": {},
   "source": [
    "## 二、列表其它方法(Sequence Types 的一些方法)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f27758e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "names的长度/names的列表元素个数： 4\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "# 1. len()查看list长度\n",
    "print(\"names的长度/names的列表元素个数：\",len(names))\n",
    "# 2. in 查看元素是否存在list当中,结果为布尔值，False/True,\n",
    "#    一般在操作list的修改和删除时会先判断\n",
    "print(\"zhichao\" in names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "309ac279",
   "metadata": {},
   "source": [
    "## 三、列表的高级方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d87f5b9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "\n",
    "# 1. copy()很重要\n",
    "# 思考，赋值是真正的复制么？ 赋值(=) <=> copy? 不等价！\n",
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ca1a6bb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [10001, 10003, 10004, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10004, 10008, 10001, 10009, 10010, 10001]\n",
      "stu_id: [10001, 10004, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n",
      "stu_id: [10001, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "# 实验 1 赋值\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "\n",
    "# 实验2 copy()\n",
    "stu_id_matedata = stu_id.copy()\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "\n",
    "# 实验3 切片实现复制\n",
    "stu_id_matedata = stu_id[:]\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1851fe5",
   "metadata": {},
   "source": [
    "|函数 |enumerate() |\n",
    "| :----: |  :----:  |\n",
    "|描述|enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列，同时列出数据和数据下标，一般用在 for 循环当中。|\n",
    "|语法|以下是 enumerate() 方法的语法:enumerate(sequence, [start=0])|\n",
    "|参数|sequence -- 一个序列、迭代器或其他支持迭代对象,start -- 下标起始位置。|\n",
    "|返回值|返回 enumerate(枚举) 对象。|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "cc2b86d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 10001\n",
      "5 10001\n",
      "8 10001\n",
      "我们想要删除pop()的值： [0, 5, 8]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "# 思考，如何取出所有位置10001的index？\n",
    "pop_values_list = []  # append方法经常用于新建列表\n",
    "for index,item in enumerate(stu_id):\n",
    "    # print(index,item)\n",
    "    if item == 10001:\n",
    "        print(index,item)\n",
    "        pop_values_list.append(index)\n",
    "print(\"我们想要删除pop()的值：\",pop_values_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42796646",
   "metadata": {},
   "source": [
    "## 四、总结及案例\n",
    "|  序号  | 方法 | 功能 |\n",
    "| :----: |  :----  |  :----  |\n",
    "| 1 |list.append(obj)|在列表末尾添加新的对象|\n",
    "| 2 |list.count(obj)|统计某个元素在列表中出现的次数|\n",
    "| 3 |list.extend(seq)|在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）|\n",
    "| 4 |list.index(obj)|从列表中找出某个值第一个匹配项的索引位置|\n",
    "| 5 |list.insert(index, obj)|将对象插入列表|\n",
    "| 6 |list.pop([index=-1])|移除列表中的一个元素（默认最后一个元素），并且返回该元素的值|\n",
    "| 7 |list.remove(obj)|移除列表中某个值的第一个匹配项|\n",
    "| 8 |list.reverse()|反向列表中元素|\n",
    "| 9 |list.sort( key=None, reverse=False)|对原列表进行排序|\n",
    "| 10 |list.clear()|清空列表|\n",
    "| 11 |list.copy()|复制列表|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b6d282ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu']\n"
     ]
    }
   ],
   "source": [
    "#1\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list1.append('Baidu')\n",
    "print (\"更新后的列表 : \", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "230314b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123 元素个数 :  2\n",
      "Runoob 元素个数 :  1\n"
     ]
    }
   ],
   "source": [
    "#2\n",
    "aList = [123, 'Google', 'Runoob', 'Taobao', 123];\n",
    "print (\"123 元素个数 : \", aList.count(123))\n",
    "print (\"Runoob 元素个数 : \", aList.count('Runoob'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "056446f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "扩展后的列表： ['Google', 'Runoob', 'Taobao', 0, 1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "#3\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list=range(5)\n",
    "list2=list # 创建 0-4 的列表 list2=list(range(5)) jupyter不支持直接创建\n",
    "list1.extend(list2)  # 扩展列表\n",
    "print (\"扩展后的列表：\", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "cad7eac6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Runoob 索引值为 1\n",
      "Taobao 索引值为 2\n"
     ]
    }
   ],
   "source": [
    "#4\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "print ('Runoob 索引值为', list1.index('Runoob'))\n",
    "print ('Taobao 索引值为', list1.index('Taobao'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fba3a543",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表插入元素后为 :  ['Google', 'Baidu', 'Runoob', 'Taobao']\n"
     ]
    }
   ],
   "source": [
    "#5\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list1.insert(1, 'Baidu')\n",
    "print ('列表插入元素后为 : ', list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f20c1e9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表现在为 :  ['Google', 'Runoob']\n",
      "列表现在为 :  ['Google']\n"
     ]
    }
   ],
   "source": [
    "#6\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list1.pop()\n",
    "print (\"列表现在为 : \", list1)\n",
    "list1.pop(1)\n",
    "print (\"列表现在为 : \", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b26aea6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表现在为 :  ['Google', 'Runoob', 'Baidu']\n",
      "列表现在为 :  ['Google', 'Runoob']\n"
     ]
    }
   ],
   "source": [
    "#7\n",
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list1.remove('Taobao')\n",
    "print (\"列表现在为 : \", list1)\n",
    "list1.remove('Baidu')\n",
    "print (\"列表现在为 : \", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "483fa81b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表反转后:  ['Baidu', 'Taobao', 'Runoob', 'Google']\n"
     ]
    }
   ],
   "source": [
    "#8\n",
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list1.reverse()\n",
    "print (\"列表反转后: \", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c122d106",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List :  ['Facebook', 'Google', 'Runoob', 'Taobao']\n"
     ]
    }
   ],
   "source": [
    "#9\n",
    "aList = ['Google', 'Runoob', 'Taobao', 'Facebook']\n",
    "aList.sort()\n",
    "print ( \"List : \", aList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2170b6c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表清空后 :  []\n"
     ]
    }
   ],
   "source": [
    "#10\n",
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list1.clear()\n",
    "print (\"列表清空后 : \", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "af98f1dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list2 列表:  ['Google', 'Runoob', 'Taobao', 'Baidu']\n"
     ]
    }
   ],
   "source": [
    "#11\n",
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list2 = list1.copy()\n",
    "print (\"list2 列表: \", list2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6029d0a",
   "metadata": {},
   "source": [
    "|  序号  | 函数 | 功能 |\n",
    "|  :----:  |  :----  |  :----  |\n",
    "|1|len()|计算序列的长度，即返回序列中包含多少个元素。|\n",
    "|2|max()|找出序列中的最大元素。注意，对序列使用 sum() 函数时，做加和操作的必须都是数字，不能是字符或字符串，否则该函数将抛出异常，因为解释器无法判定是要做连接操作（+ 运算符可以连接两个序列），还是做加和操作。|\n",
    "|3|min()|找出序列中的最小元素。|\n",
    "|4|list()|将序列转换为列表。|\n",
    "|5|str()|将序列转换为字符串。|\n",
    "|6|sum()|计算元素和。|\n",
    "|7|sorted()|对元素进行排序。|\n",
    "|8|reversed()|反向序列中的元素。|\n",
    "|9|enumerate()|将序列组合为一个索引序列，多用在 for 循环中。|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f21db4ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "#1\n",
    "str = \"runoob\"\n",
    "print( len(str) )          # 字符串长度\n",
    " \n",
    "l = [1,2,3,4,5]\n",
    "print( len(l) )            # 列表元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "85dfdd34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "#2\n",
    "l = [1,2,3,4,5]\n",
    "h =max(l)\n",
    "print(h)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "140df5ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "#3\n",
    "l = [1,2,3,4,5]\n",
    "h =min(l)\n",
    "print(h)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "31ce8348",
   "metadata": {},
   "outputs": [],
   "source": [
    "#4\n",
    "#aTuple = (123, 'runoob', 'google', 'abc');\n",
    "# aList = list(aTuple)\n",
    " \n",
    "# print (\"列表元素 : \")\n",
    "# print(aList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "f4fbb611",
   "metadata": {},
   "outputs": [],
   "source": [
    "#5\n",
    "# s = 'RUNOOB'\n",
    "# str(s)\n",
    "# dict = {'runoob': 'runoob.com', 'google': 'google.com'};\n",
    "# str(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6dca2ea3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#6\n",
    "sum([0,1,2])    \n",
    "sum((2, 3, 4), 1)        # 元组计算总和后再加 1\n",
    "sum([0,1,2,3,4], 2)      # 列表计算总和后再加 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "6eb41cd9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#7\n",
    "students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]\n",
    "sorted(students, key=lambda s: s[2], reverse=True)       # 按年龄降序降序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "5cadbfe1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['xyz', 'abc', 'zara', 'xyz', 123]\n"
     ]
    }
   ],
   "source": [
    "#8\n",
    "aList = [123, 'xyz', 'zara', 'abc', 'xyz']\n",
    "\n",
    "aList.reverse()\n",
    "print(aList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "7079da8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 这\n",
      "1 是\n",
      "2 一个\n",
      "3 测试\n"
     ]
    }
   ],
   "source": [
    "#9\n",
    "list1 = [\"这\", \"是\", \"一个\", \"测试\"]\n",
    "for index, item in enumerate(list1):\n",
    "    print(index,item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf22e139",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
